using Microsoft.AspNetCore.Mvc;
using TMS.Application.Common.Interface;
using TMS.Application.RequestDto;
using TMS.Application.Utils;
using TMS.Domain.Entity;

namespace TMS.Api.Controllers
{
    [ApiController]
    public class ClassListController : ControllerBase
    {
        private readonly IRepository<Class> _Class;
        private readonly IRepository<Students> _Students;
        private readonly IRepository<Teachers> _Teachers;
        private readonly IRepository<College> _College;
        private readonly IRepository<Major> _Major;
        private readonly IRepository<Term> _Term;
        private readonly IRepository<Course> _Course;

        public ClassListController(IRepository<Course> Course, IRepository<Term> Term, IRepository<Major> Major, IRepository<College> College, IRepository<Class> Class, IRepository<Students> Students, IRepository<Teachers> Teachers)
        {
            _Course = Course;
            _Term = Term;

            _Class = Class;
            _Students = Students;
            _Teachers = Teachers;
            _College = College;
            _Major = Major;
        }
        public class MajorLists
        {
            /// <summary>
            /// 专业ID
            /// </summary>
            /// <value></value>
            public Guid MajorId { get; set; }

            /// <summary>
            ///  专业名称
            /// </summary>
            /// <value></value>
            public string? MajorName { get; set; }

        }
        public class CollegeLists
        {
            /// <summary>
            /// 学院ID
            /// </summary>
            /// <value></value>
            public Guid CollegeId { get; set; }

            /// <summary>
            ///  学院名称
            /// </summary>
            /// <value></value>
            public string? CollegeName { get; set; }

            /// <summary>
            /// 学年
            /// </summary>
            /// <value></value>
            public int SchoolYear { get; set; }

            /// <summary>
            /// 专业列表
            /// </summary>
            /// <value></value>
            public List<MajorLists> MajorLists { get; set; }
        }
        //获取管理端班级列表级联数据
        [HttpGet("/ClassListQuery")]
        public async Task<object> ClassListQuery()
        {
            //创建一份新的学院列表
            List<CollegeLists> CollegeLists = new List<CollegeLists>();
            var College = _College.Table.ToList();
            var StuClass = _Class.Table.ToList();
            var Major = _Major.Table.ToList();
            foreach (var item in College)
            {
                //创建一份新的专业列表
                List<MajorLists> MajorLists = new List<MajorLists>();
                var College_Major = Major.Where(x => x.CollegeId == item.Id).ToList();
                foreach (var items in College_Major)
                {
                    //加到专业里
                    MajorLists.Add(new MajorLists
                    {
                        MajorId = items.Id,
                        MajorName = items.MajorName,
                    });
                }
                //加到学院里
                CollegeLists.Add(new CollegeLists
                {
                    CollegeId = item.Id,
                    CollegeName = item.CollegeName,
                    MajorLists = MajorLists,
                    SchoolYear = item.CreatedAt.Year
                });
            }

            return CollegeLists;
        }
        [HttpGet("/ClassList")]
        public async Task<object> ClasstList(Guid id)
        {
            //学院列表
            var College = _College.Table.ToList();

            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();
            //学生列表
            var Student = _Students.Table.ToList();


            List<object> list = new List<object>();
            foreach (var item in Class)
            {
                var ClassId = Class.Where(x => x.Id == item.Id).FirstOrDefault();
                var Class_Major = Major.Where(x => x.Id == ClassId.MajorId).FirstOrDefault();
                var Class_College = College.Where(x => x.Id == Class_Major.CollegeId).FirstOrDefault();
                var counts = Student.Where(x => x.ClassId == item.Id).Count();

                list.Add(new
                {
                    Id = item.Id,
                    CollegeName = Class_College.CollegeName,
                    MajorName = Class_Major.MajorName,
                    className = item.ClassName,
                    counts
                });
            }
            return new
            {
                code = 200,
                msg = "成功",
                data = list
            };
        }
        [HttpGet("/ClassList/{id}")]
        public async Task<object> MajorClasstList(Guid id)
        {

            //学院列表
            var College = _College.Table.ToList();

            //专业列表
            var Major = _Major.Table.ToList();
            //班级列表
            var Class = _Class.Table.ToList();
            //学生列表
            var Student = _Students.Table.ToList();

            var ClassList = Major.Where(x => x.Id == id).FirstOrDefault();
            var classid = _Class.Table.Where(x => x.MajorId == ClassList.Id).ToList();
            if (classid != null)
            {
                List<object> list = new List<object>();
                List<object> count = new List<object>();
                foreach (var item in classid)
                {
                    var ClassId = classid.Where(x => x.MajorId == id).ToList();
                    // var classNames = ClassId.Select(x=>x.ClassName).FirstOrDefault();
                    var Class_Major = Major.Where(x => x.Id == ClassList.Id).FirstOrDefault();
                    var Class_College = College.Where(x => x.Id == Class_Major.CollegeId).FirstOrDefault();
                    var counts = Student.Where(x => x.ClassId == item.Id).Count();
                    list.Add(new
                    {
                        Id = item.Id,
                        className = item.ClassName,
                        CollegeName = Class_College.CollegeName,
                        MajorName = Class_Major.MajorName,
                        counts
                    });
                }
                return new
                {
                    code = 200,
                    msg = "成功",
                    data = list
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "白给",
                    data = "null"
                };
            }

        }

        //新增专业项的班级
        [HttpPost("/AddMajorClass")]
        public async Task<object> AddMajorClass(Class stuClassDto)
        {
            //所有的专业
            var Major = _Major.Table.ToList();

            var terms = _Term.Table.ToList();
            //所有的班级
            var StuClass = _Class.Table.ToList();
            //获取到前端传回的专业ID来寻找该专业
            var Major_StuClass = Major.Where(x => x.Id == stuClassDto.MajorId).FirstOrDefault();
            // List<Class> list = new List<Class>();

            var ClassMajorName = StuClass.Where(x => x.MajorId == Major_StuClass.Id).ToList();
            var ObjName = ClassMajorName.Where(x => x.ClassName == stuClassDto.ClassName).ToList();
            if (ObjName.Count >= 1)
            {
                return new
                {
                    code = 400,
                    msg = "该班级已经存在",
                    data = "null"
                };
            }
            else
            {
                try
                {
                    await _Class.AddAsync(stuClassDto);

                    // List<Class> list = new List<Class>();

                    var stuClass = _Class.Table.ToList();
                    var classMajorName = stuClass.Where(x => x.MajorId == Major_StuClass.Id).ToList();
                    var className = classMajorName.FirstOrDefault(x => x.ClassName == stuClassDto.ClassName);

                    var text = "{'0':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'1':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'2':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'3':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'4':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'5':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'6':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'7':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'8':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}},'9':{'0':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'1':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'2':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'3':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'4':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'5':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'},'6':{'SubjectName':'','Place':'','SubjectId':'00000000-0000-0000-0000-000000000000'}}}";

                    List<Course> list = new List<Course>();

                    foreach (var item in terms)
                    {
                        var data = new Course
                        {

                            ClassId = className.Id,

                            TermId = item.Id,

                            CourseText = text

                        };

                        list.Add(data);
                    }

                    await _Course.AddBulkAsync(list);

                    return new
                    {
                        code = 200,
                        msg = "成功",
                        data = stuClassDto
                    };
                }
                catch
                {
                    return new
                    {
                        code = 400,
                        msg = "失败",
                        data = stuClassDto
                    };

                }



            }

        }
        public class ClassDataDto
        {

            public Guid id { get; set; }

            public string? ClassName { get; set; }

        }
        //修改班级名称
        [HttpPut("/EditClass")]
        public async Task<object> EditClass(ClassDataDto classDataDto)
        {
            var StuClassId = await _Class.GetByIdAsync(classDataDto.id);
            if (StuClassId != null)
            {
                StuClassId.ClassName = classDataDto.ClassName;
                await _Class.UpdateAsync(StuClassId);
                return new
                {
                    code = 200,
                    msg = "修改成功！！！",
                    data = ""
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "出错了不该来的地方",
                    data = ""
                };
            }

        }
        public class collegeDate
        {

            public Guid CollegeId { get; set; }

            public string? CollegeName { get; set; }

        }
        //修改学院名称
        [HttpPut("/Editcollege")]
        public async Task<object> Editcollege(collegeDate collegeDate)
        {
            var CollegeId = await _College.GetByIdAsync(collegeDate.CollegeId);
            if (CollegeId != null)
            {
                CollegeId.CollegeName = collegeDate.CollegeName;
                await _College.UpdateAsync(CollegeId);
                return new
                {
                    code = 200,
                    msg = "修改成功！！！",
                    data = ""
                };
            }
            else
            {
                return new
                {
                    code = 400,
                    msg = "出错了不该来的地方",
                    data = ""
                };
            }

        }

    }

}